var fangdai = {
	els: {
		//问题列表
		qList: $("#QuestionList"),
		//下一步
		next: $("#BtnNext"),
		//上一步
		pre: $("#BtnPre"),
		//专家提示
	},
	init: function(){
		var me = this;
		//绑定下一步、上一步操作事件
		me.bindAction();
		fangdai.drawList(fangdai.getCity());
	},	
	//绑定下一步、上一步事件操作
	bindAction: function(){
		var me = this;
		me.els.next.click(function(){
			var _selRadio = $('input[name=' + me.varList.varName + ']:checked');
			var _value = _selRadio.val();
			var _text = _selRadio.attr("desc");
			me.next(_value, _text);
		});
		me.els.pre.click(function(){

		});
	},
	//下一步执行
	next: function(id,text){
		var me = this;
		if(me.varList.varName == "city_id"){
			me.varList.cityId = id;
		}else{
			//追加计算结果列表
			me.resultList[me.varList.eleId] = {
				id: me.varList.eleId,
				value: id,
				varName: me.varList.varName,
				varText: text,
				title: me.varList.title,
			};
		}

		var result = me.calcu();
		//如果是继续
		if(result.type == "goon"){
			return me.getElementList();
			//me.getElementList();
		}			
		//如果是成功
		if(result.type == "success"){
			var _result = 0;
			//如果成功后，则转身num_have处理逻辑
			if(me.varList.token == "loan_index"){
				me.statusList.nowCount.key = true;
				me.statusList.nowCount.data = result.value;
			}
			if(me.varList.token == "num_have"){
				me.statusList.nowHave.key = true;
				me.statusList.nowHave.data = result.value;
				me.varList.token = "loan_index";
			}
			if(me.varList.token == "can_buy"){
				me.statusList.canBuy.key = true;
				me.statusList.canBuy.data = result.value;
				me.varList.token = "num_have";
			}
			//console.log("状态列表：" + JSON.stringify(me.statusList));
			if(me.statusList.nowCount.key == true){
				console.log(me.resultList);
				console.log("你可以购买：" + (me.statusList.canBuy.data - me.statusList.nowHave.data));
				console.log("此次购买算做第" + (me.statusList.nowCount.data) + "套房");
			}else{
				me.calcu();
				return me.getElementList();
			}
		}
	},
	//数据分层
	dataList:{
		//城市列表
		city: data.hpcity,
		//选项列表
		element: data.city_element,
		//政策列表
		policy: data.city_policy
	},
	//状态列表
	statusList: {
		//能买几套房
		canBuy: {
			key: false,
			data: 0
		},
		//拥有几套房
		nowHave: {
			key: false,
			data: 0
		},
		//此次购买算第几套
		nowCount: {
			key: false,
			data: 0
		}
	},
	//数据变量
	varList: {
		token: "can_buy",
		cityId: "10000",
		varName: "city_id",
		varValue: "",
		varText: "",
		title: "",
		eleId: "",
		canBuy: 0
	},
	//计算结果数据结构
	resultList:{},
	/*
		{
			id: "",步骤id
			title: "",//标题描述
			answer: ""//回答值
		}
	*/
	//获取城市列表
	getCity: function(){
		var _result = {
			title: "您所在的城市",
			data: []
		};
		var city = data.hpcity;
		$(city).each(function(index, item){
			_result.data.push({
				value: item.id,
				desc: item.name
			});
		});
		this.varList.varName = "city_id";
		return _result;
	},
	//获取枚举列表
	getElementList: function(){
		//console.log(id);
		var me = this;
		//计算
		id = me.varList.eleId;
		cityid = this.varList.cityId;
		var elementData = this.dataList.element;
		var _result = {
			title: "",
			data: []
		};
		$(elementData).each(function(index, item){
			if(item.id == id && item.city_id == cityid){
				//console.log("title:" + item.title);
				_result.title = item.title;
				_result.data = item.data;
				me.varList.varName = item.var_name;
			}
		});
		//console.log(!_result.data);
		if(_result.data.length == 0){
			$(elementData).each(function(index, item){
				if(item.id == id && item.city_id == "10000"){
					//console.log("title:" + item.title);
					_result.title = item.title;
					_result.data = item.data;
					me.varList.varName = item.var_name;
				}
			});
		}
		//console.log(_result);
		me.varList.title = _result.title;
		this.drawList(_result);
		return _result;
	},
	//获取下一个问题id
	getNextQ: function(data){
		var me = this;
		token = me.varList.token;
		var branch = {};
		//计算问题出现数
		$(data).each(function(index, item){
			//console.log(item);
			$(item.require).each(function(subindex,subitem){
				//console.log(subitem);
				if(branch[subitem.id]){
					branch[subitem.id] += 1;
				}else{
					branch[subitem.id] = 1;
				}
			});
		});
		//步骤id按出现次数排序
		var _dataList = [];
		for(var key in branch){
			_dataList.push({
				id: key,
				data: branch[key]
			});
		}
		_dataList.sort(function(a,b){return a["data"]>b["data"]?1:-1});
		_dataList.reverse();
		//console.log("resultList:" + JSON.stringify(me.resultList));
		for(var i = 0;i<_dataList.length; i++){
			//console.log("resultID:" + _dataList[i].id);
			if(!me.resultList[_dataList[i].id]){
				//返回问题id
				return _dataList[i].id;
			}
		}
	},
	//计算结果逻辑
	calcu: function(){
		var me = this;
		token = me.varList.token;
		var branch = {};
		var policyList = this.dataList.policy;
		var policy;
		var _resultData = {};
		var _passKey = false;
		//输出
		var _result = {
			type: "",
			value: ""
		};

		//console.log(me.varList.cityId);
		//获取匹配城市项
		$(policyList).each(function(index, item){
			//匹配城市ID和token
			if((me.varList.cityId == item.city_id) && (token == item.token)){
				policy = item;
			}
		});
		//如果没有找到匹配项，则按默认匹配
		if(!policy){
			$(policyList).each(function(index, item){
				//匹配城市ID和token
				if((item.city_id == "10000") && (token == item.token)){
					policy = item;
				}
			});	
		}

		var emptyItemList = [];
		//计算结果
		//$(policy.data).each(function(index, item){
		for(var index = 0;index < policy.data.length; index++){
			var item = policy.data[index];
			//console.log(item);
			var _branchResult = true;
			//console.log("relength:" + item.require.length);

			for(var subindex = 0; subindex<item.require.length; subindex++){
				var subitem = item.require[subindex];
				if(me.resultList[subitem.id]){
					//console.log("data：" + subitem.data);
					//console.log(me.resultList[subitem.id]);
					if( subitem.data.indexOf(me.resultList[subitem.id].value) == -1){
						//console.log("失败");
						_branchResult = false;
						break;
					}
				}
				else if(_branchResult == true){
					_branchResult = 'empty';
				}
			}
			//当计算成功后
			if(_branchResult == true){
				//console.log(item.result_val);
				_result = {
					type: "success",
					value: item.result_val
				};

				return _result;
			}else if(_branchResult == 'empty'){
				emptyItemList.push(item);
			}

		}
		//如果失败
		if(emptyItemList.length == 0){
			_result = {
				type: "fail",
				value: "您不能买房子"
			};

			return _result;
		}
		
		//如果继续计算
		var _eleId = me.getNextQ(emptyItemList);
		me.varList.eleId = _eleId;
		_result = {
			type: "goon",
			value: me.varList.eleId
		};

		return _result;
	},
	//渲染列表
	drawList: function(data){
		//console.log(data);
		var me = this;

		var _listDom = "";
		var _cityOther = "";
		$(data.data).each(function(index, item){
			var _itemDom = me.tpl.pListItem
							.replace("{name}", me.varList.varName)
							.replace("{value}", item.value)
							.replace(/{text}/g, item.desc);
			_listDom += _itemDom;
		});
		//console.log(data.data);
		me.els.qList.html(_listDom);
		return data.data;
	},
	//模板
	tpl: {
		//问题列表模板
		pListItem: '<p{title}><label><input type="radio" name="{name}" value="{value}" desc="{text}" /></label><span class="label-text">{text}</span></p>'
	}
};

fangdai.init();

